# Create your views here.
import json

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from apps.PO.CoursePO import CoursePO
from apps.PO.StudentPO import StudentPO
from apps.PO.UserPO import UserPO
from apps.Service.CourseService import CourseService
from apps.Service.StudentService import StudentService
from apps.Service.UserService import UserService
from apps.Utils.ResultfulApi import resultful_api


# 登录逻辑
@csrf_exempt
def login(request):
    # 接收前端传来的json数据
    form = json.loads(request.body)
    # 实例化UserPO
    user_po = UserPO(user_name=form['username'], password=form['password'])
    # 实例化UserService
    user_service = UserService(user_po)
    # 验证数据
    if form:
        # 登录校验
        result = user_service.login()
        if result:
            # 登录成功
            return JsonResponse(resultful_api(1, "登录成功", result))
        else:
            # 登录失败
            return JsonResponse(resultful_api(0, "登录失败，用户名或密码错误", {}))


# 注册逻辑
@csrf_exempt
def register(request):
    form = json.loads(request.body)
    if form:
        # 实例化UserPO
        user_po = UserPO(user_name=form['username'], password=form['password'])
        # 实例化UserService
        user_service = UserService(user_po)
        # 注册校验
        result = user_service.register()
        if result:
            # 注册成功
            return JsonResponse(resultful_api(1, "注册成功", result))
        else:
            # 注册失败
            return JsonResponse(resultful_api(0, "注册失败，用户名或密码已存在", {}))


# 更新用户信息
@csrf_exempt
def update_user_info(request):
    # 接收前端传来的json数据为字典
    form = request.POST.dict()
    if form:
        # 实例化UserPO
        user_po = UserPO(user_id=form['user_id'], user_name=form['username'], password=form['password'])
        # 实例化UserService
        user_service = UserService(user_po)
        # 更新用户信息
        result = user_service.update_user_info()
        if result:
            # 更新成功
            return JsonResponse(resultful_api(200, "更新成功", result))
        else:
            # 更新失败
            return JsonResponse(resultful_api(400, "更新失败，请检查用户名或密码", {}))


# 根据user_id删除用户
@csrf_exempt
def delete_user(request):
    # 接收前端传来的json数据为字典
    form = json.loads(request.body)
    if form:
        # 实例化UserPO
        user_po = UserPO(user_id=form['user_id'])
        # 实例化UserService
        user_service = UserService(user_po)
        # 删除用户
        result = user_service.delete_user()
        if result:
            # 删除成功
            return JsonResponse(resultful_api(200, "删除成功", result))
        else:
            # 删除失败
            return JsonResponse(resultful_api(400, "删除失败，请检查用户id", {}))


# 根据course_name更新课程信息
@csrf_exempt
def update_course_api(request):
    # 接收前端传来的json数据为字典
    form = request.POST.dict()
    if form:
        # 实例化CousePO
        course_po = CoursePO(course_name=form['course_name'], cat=form['cat'], teacher=form['teacher'])
        # 实例化CourseService
        course_service = CourseService(course_po)
        # 更新课程信息
        result = course_service.update_course_by_name()
        if result:
            # 更新成功
            return JsonResponse(resultful_api(200, "更新成功", result))
        else:
            # 更新失败
            return JsonResponse(resultful_api(400, "更新失败，请检查课程名称或教师", {}))


# 根据course_name删除课程
@csrf_exempt
def del_course_api(request):
    # 接收前端传来的json数据为字典
    form = json.loads(request.body)
    if form:
        # 实例化CousePO
        course_po = CoursePO(course_name=form['course_name'])
        # 实例化CourseService
        course_service = CourseService(course_po)
        # 删除课程
        result = course_service.delete_course_by_name()
        if result:
            # 删除成功
            return JsonResponse(resultful_api(200, "删除成功", result))
        else:
            # 删除失败
            return JsonResponse(resultful_api(400, "删除失败，请检查课程名称", {}))


# 课程推荐接口
@csrf_exempt
def course_recommend(request):
    form = json.loads(request.body)
    # 获取学生id
    student_id = form['stu_id']
    print(student_id)
    # 实例化学生
    stu_po = StudentPO(stu_id=student_id)
    # 实例化CourseService
    student_service = StudentService(stu_po)
    # 课程推荐
    result = student_service.recommend_course()

    recommends = []
    for item in result:
        recommends.append(item[2])
    recommends = list(set(recommends))[:5]
    if result:
        # 推荐成功
        return JsonResponse(resultful_api(200, "推荐成功", recommends))
